/*
 * csdra all rights received.
 */
package net.csdra.cms.wordpress.service;

import com.google.common.base.Throwables;
import net.csdra.cms.wordpress.repository.WordPressAdapter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import net.csdra.cms.api.model.Category;
import net.csdra.cms.api.model.Post;
import net.csdra.cms.api.service.CmsService;
import net.csdra.cms.wordpress.model.WpPosts;
import net.csdra.cms.wordpress.model.WpTerms;
import org.springframework.beans.BeanUtils;

/**
 *
 * @author Didy
 */
public class WordpressCmsServiceImpl implements CmsService {

    @Resource
    WordPressAdapter wordPressAdapter;

    private <T> T convert(Object src, Class<T> targetClass) {
        try {
            T target = targetClass.newInstance();
            BeanUtils.copyProperties(src, target);
            return target;
        } catch (InstantiationException | IllegalAccessException ex) {
            Logger.getLogger(WordpressCmsServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw Throwables.propagate(ex);
        }
    }

    private <T> Collection<T> convert(Iterable src, Class<T> targetClass) {
        Collection<T> list = new ArrayList<>();
        for (Object src1 : src) {
            T target = convert(src1, targetClass);
            list.add(target);
        }
        return list;
    }

    @Override
    public Post getPost(int blogId, Long id) {

        WpPosts post = wordPressAdapter.getPost(blogId, id);
        return convert(post, Post.class);
    }

    @Override
    public Map<String, String> getOptions(int blogId) {
        return wordPressAdapter.getOptions(blogId);
    }

    @Override
    public Collection<Post> getPostsByCategory(int blogId, Long categoryId, int offset, int limit) throws IOException {
        Collection<WpPosts> postsByCategory = wordPressAdapter.getPostsByCategory(blogId,categoryId, offset, limit);
        return convert(postsByCategory, Post.class);
    }

    @Override
    public Post getPage(int blogId, String name) {
        WpPosts post = wordPressAdapter.getPage(blogId,name);
        return convert(post, Post.class);
    }

    @Override
    public Collection<Post> getSubpages(int blogId, Long id) {
        Collection<WpPosts> subpages = wordPressAdapter.getSubpages(blogId,id);
        return convert(subpages, Post.class);
    }

    @Override
    public String getPostCover(int blogId, Long id) {
        return wordPressAdapter.getPostCover(blogId,id);
    }

    @Override
    public Category getCategory(int blogId, Long id) {
        WpTerms category = wordPressAdapter.getCategory(blogId,id);
        return convert(category, Category.class);
    }

    @Override
    public Category getCategory(int blogId, Post post) {
        WpTerms category = wordPressAdapter.getCategory(blogId,post);
        return convert(category, Category.class);
    }

    @Override
    public Collection<Category> getCategories(int blogId, Long id) {
        Collection<WpTerms> categories = wordPressAdapter.getCategories(blogId,id);
        return convert(categories, Category.class);
    }

    @Override
    public String hello() {
        return "hello";
    }

}
